#include <stdio.h>
#include "linkedlist_basic.h"
#include "linkedlist_countdata.h"

#define NUMELEMENT 10

// Count the number of nodes that contains a specific data
int countdata(NODE* head, int searchFor) {
    int count = 0;
    NODE* currentNode = head;
    while (currentNode != NULL) {
        if (currentNode->data == searchFor) {
            count++;
        }
        currentNode = currentNode->next;
    }
    return count;
}

void test_linklist_countdata() {
    NODE* head = NULL;
	int data[NUMELEMENT] = {1, 3, 4, 1, 8, 6, 10, 3, 0, 1};
	int searchFor;
	
	for (int i = 0; i < NUMELEMENT; i++ ) {
		printf("Insert %d at the front of the list\n", data[i]);
		insertInFront(&head, data[i]);
	}
	
	searchFor = 1;
	printf("\nNumber of nodes in this list that contains %d = %d\n", searchFor, countdata(head, searchFor));
	searchFor = 2;
	printf("\nNumber of nodes in this list that contains %d = %d\n", searchFor, countdata(head, searchFor));
	searchFor = 3;
	printf("\nNumber of nodes in this list that contains %d = %d\n", searchFor, countdata(head, searchFor));
}
